<?php
namespace Wap\Service;
use Think\Model;

class CustomerService
{
    private static $STATUS_ID = 1;
    private static $STATUS_OPENID = 2;

    private static $ID = "customer.id";
    private static $openID = "customer.openid";
    //private static $isOnline = "customer.isonline";

    // private static $tableName = "uploader";
    private static $tableName = "customer";

    public static function getID()
    {
        return self::getValueBy( self::$ID );
    }

    public static function setID( $id )
    {
        //S("debug_id", $id, 2400 );
        $old_id = self::getValueBy( self::$ID );
        //S( "debug_idoldid", array( $id, $old_id ), 3600 );
        if( $id == null ){
            self::expireValueBy( self::$STATUS_ID, self::$ID, null );
        //}else if( $old_id == null ){
            //self::setValueBy( self::$ID, $id );
        }else if( $old_id != $id ){
            self::update( self::$STATUS_ID, self::$ID, $id, $old_id );
        }
    }

    public static function getOpenID()
    {
        return self::getValueBy( self::$openID );
    }

    public static function setOpenID( $openid )
    {
        $old_openID = self::getValueBy( self::$openID );
        trace( $_SESSION, "session 状态信息", "SQL" );
        trace( array( "old_openID" => $old_openID, "openid" => $openid ), "用户状态信息", "SQL" );
        if( $openid == null && $old_openID !== null ){
            $condition['wecha_id'] = $old_openID;
            D( self::$tableName )->where( $condition )->setField( "wecha_id", null );
            self::expireValueBy( self::$STATUS_OPENID, self::$openID, null );
        //}else if( $old_openID == null ){
            //self::setValueBy( self::$openID, $openid );
        }else if( $old_openID != $openid ){
            self::update( self::$STATUS_OPENID, self::$openID, $openid, $old_openID );
        }
    }

    public static function check()
    {
        //判定session中是否存在
        $online_status = 0;
        $condition['id'] = self::getValueBy( self::$ID );
        $condition['openid'] = self::getValueBy( self::$openID );
        if( !$condition['openid'] || !$condition['id'] )
        {
            $online_status = 0;
            return $online_status;
        }

        //判定数据库中是否存在
        //$mode = D( self::$tableName );
        $exist = D( self::$tableName )->where( $condition )->select();
        //S( "debug_exist", $exist, 3600 );
        if( is_array( $exist ) && count( $exist ) ){
            $online_status = 1;
        }

        return $online_status;
        //$sub_condition = array_diff( array( "id" => "" ), $condition );
        /*$is_weixin_user = $mode->where( $sub_condition )->find();
        if( $is_weixin_user )
        {
             $mode->where( $sub_condition )->setField( "wecha_id", self::getValueBy( self::$openID ) );
             $online_status = 1;
             return $online_status;
        }

        if( !$condition['id'] )
        {
            $online_status = 0;
            return $online_status;
        }*/
    }

    private static function update( $field = -1, $key, $value, $old_value )
    {
        if( !$old_value )
        {
            self::setValueBy( $key, $value );
        }

        switch( $field )
        {
            case self::$STATUS_ID:
                if( !!$old_value ){
                    $condition['id'] = $old_value;
                    D( self::$tableName )->where( $condition )->setField( "wecha_id", null );
                }
                $condition['id'] = $value;
                D( self::$tableName )->where( $condition )->setField( "wecha_id", self::getValueBy( self::$openID ) );
                self::setValueBy( self::$ID, $value );
                break;
            case self::$STATUS_OPENID:
                if( !$old_value ){
                    $condition['wecha_id'] = $value;
                    $id = D( self::$tableName )->where( $condition )->getField( "id" );
                    self::setValueBy( self::$ID, $id );
                    self::setValueBy( self::$openID, $value );
                }else{//TODO: never exec
                    $condition['wecha_id'] = $old_value;
                    trace( $condition, 'never exec ?', 'SQL' );
                    D( self::$tableName )->where( $condition )->setField( "wecha_id", null );//TODO: maybe cause error
                    $condition['id'] = self::getValueBy( self::$ID );
                    D( self::$tableName )->where( $condition )->setField( "wecha_id", $value );
                }
                break;
            default: break;
        }
    }

    //////////////////////////////////////////////////
    private function getValueBy( $key, $scope )//TODO: scope not support now
    {
        return session( $key );
    }

    private function setValueBy( $key, $value )
    {
         session( $key, $value );
    }

    private function expireValueBy( $field, $key, $value )
    {
        switch( $field )
        {
            case self::$STATUS_ID:
                session( $key, null );
                break;
            case self::$STATUS_OPENID:
                $condition['id'] = self::getValueBy( self::$ID );
                D( self::$tableName )->where( $condition )->setField( "wecha_id", null );
                session( $key, null );
                break;
            default: break;
        }
    }
}
